//
//  ResizeViewChange.m
//  DemoResizeView
//
//  Created by duc nguyen van on 8/17/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "ResizeViewChange.h"
#define EDGE_CHECK 20
#define WIDTH_LIMIT 100
#define HEIGHT_LIMIT 100

@implementation ResizeViewChange
@synthesize delegate;

#pragma mark -
#pragma mark some need methods
-(void)addCheckWith:(UIView*)check andCenter:(CGPoint)cen andTag:(int)tagC{
	check.bounds = CGRectMake(0, 0, EDGE_CHECK, EDGE_CHECK);
	check.backgroundColor = [UIColor redColor];
	check.center = cen;
	check.exclusiveTouch = YES;
	check.layer.cornerRadius = EDGE_CHECK/2;
	check.layer.borderColor = [[UIColor whiteColor] CGColor];
	check.layer.borderWidth = 2.0;
	check.layer.shadowRadius = 2;
    check.layer.shadowOpacity = 0.5;
	check.tag = tagC;
}

-(void)addConfigAllView{
	[self addCheckWith:checkOne andCenter:CGPointMake(EDGE_CHECK/2, EDGE_CHECK/2) andTag:1];
	[self addCheckWith:checkTwo andCenter:CGPointMake(self.frame.size.width / 2, EDGE_CHECK/2) andTag:2];
	[self addCheckWith:checkThree andCenter:CGPointMake(self.frame.size.width- EDGE_CHECK/2, EDGE_CHECK/2) andTag:3];
	[self addCheckWith:checkFour andCenter:CGPointMake(self.frame.size.width - EDGE_CHECK/2, self.frame.size.height /2) andTag:4];
	[self addCheckWith:checkFive andCenter:CGPointMake(self.frame.size.width - EDGE_CHECK/2, self.frame.size.height - EDGE_CHECK/2) andTag:5];
	[self addCheckWith:checkSix andCenter:CGPointMake(self.frame.size.width /2, self.frame.size.height - EDGE_CHECK/2) andTag:6];
	[self addCheckWith:checkSeven andCenter:CGPointMake(EDGE_CHECK/2, self.frame.size.height - EDGE_CHECK/2) andTag:7];
	[self addCheckWith:checkEight andCenter:CGPointMake(EDGE_CHECK/2, self.frame.size.height /2) andTag:8];	
}

#pragma mark -
#pragma mark handle check event

-(void)addNewFrame:(CGRect)fra andVex:(CGPoint)vex andTag:(int)tagV{
	if (fra.size.width > WIDTH_LIMIT && fra.size.height > HEIGHT_LIMIT) {
		self.frame = CGRectMake(fra.origin.x, fra.origin.y, fra.size.width, fra.size.height);
		[self addConfigAllView];
		[delegate scaleViewWithVex:tagV andVex:vex];
	}
}

-(void)panMoveThisView:(UIPanGestureRecognizer*)pan{
	if (isRisize == NO) {
		if([pan state] == UIGestureRecognizerStateBegan){
			BPoint = [pan locationInView:self];
		}
		
		EPoint = [pan locationInView:self];
		
		CGPoint vex;
		vex.x = EPoint.x - BPoint.x;
		vex.y = EPoint.y - BPoint.y;
		
		[delegate checkTouchesMoved:pan andVex:vex];
		
		//set move
		CGPoint cen = self.center;
		cen.x += vex.x;
		cen.y += vex.y;
		self.center = cen;
	}
}

-(CGSize)sizeMoveViewWithWidth:(float)width andHeight:(float)height{
	return CGSizeMake(width, height);
}

-(void)panMoveViewOne:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:((self.frame.origin.x + self.frame.size.width) - oriPoint.x)
										andHeight:((self.frame.origin.y + self.frame.size.height) - oriPoint.y)];
		
		[self addNewFrame:CGRectMake(oriPoint.x , oriPoint.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - self.frame.origin.x, oriPoint.y - self.frame.origin.y) 
				   andTag:1];
	}
}

-(void)panMoveViewTwo:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		oriPoint.x -= self.frame.size.width / 2;
	
		CGSize size = [self sizeMoveViewWithWidth:self.frame.size.width
										andHeight:((self.frame.origin.y + self.frame.size.height) - oriPoint.y)];	

		[self addNewFrame:CGRectMake(self.frame.origin.x , oriPoint.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - self.frame.origin.x, oriPoint.y - self.frame.origin.y) 
			   andTag:2];
	}
}

-(void)panMoveViewThree:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:oriPoint.x - self.frame.origin.x
										andHeight:((self.frame.origin.y + self.frame.size.height) - oriPoint.y)];	
	
		[self addNewFrame:CGRectMake(self.frame.origin.x , oriPoint.y, size.width, size.height) 
				   andVex:CGPointMake((oriPoint.x - (self.frame.origin.x + self.frame.size.width)), oriPoint.y - self.frame.origin.y) 
				   andTag:3];
	}
}

-(void)panMoveViewFour:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];	
		CGSize size = [self sizeMoveViewWithWidth:self.frame.size.width + (oriPoint.x - (self.frame.origin.x + self.frame.size.width))
										andHeight:self.frame.size.height];
	
		[self addNewFrame:CGRectMake(self.frame.origin.x , self.frame.origin.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - (self.frame.origin.x + self.frame.size.width), oriPoint.y - (self.frame.origin.y + self.frame.size.width / 2))
				   andTag:4];
	}
}

-(void)panMoveViewFive:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:self.frame.size.width + (oriPoint.x - (self.frame.origin.x + self.frame.size.width))
										andHeight:self.frame.size.height + (oriPoint.y - (self.frame.origin.y + self.frame.size.height))];
	
		[self addNewFrame:CGRectMake(self.frame.origin.x , self.frame.origin.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - (self.frame.origin.x + self.frame.size.width), oriPoint.y - (self.frame.origin.y + self.frame.size.height)) 
				   andTag:5];
	}
}

-(void)panMoveViewSix:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){	
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:self.frame.size.width
										andHeight:self.frame.size.height + (oriPoint.y - (self.frame.origin.y + self.frame.size.height))];

		[self addNewFrame:CGRectMake(self.frame.origin.x , self.frame.origin.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - (self.frame.origin.x + self.frame.size.width), oriPoint.y - (self.frame.origin.y + self.frame.size.height)) 
				   andTag:6];
	}
}

-(void)panMoveViewSeven:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:(self.frame.origin.x + self.frame.size.width) - oriPoint.x
										andHeight:oriPoint.y - self.frame.origin.y];
	
		[self addNewFrame:CGRectMake(self.frame.origin.x + (oriPoint.x - self.frame.origin.x), self.frame.origin.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - self.frame.origin.x, oriPoint.y - (self.frame.origin.y + self.frame.size.height)) 
				   andTag:7];
	}
}

-(void)panMoveViewEight:(UIPanGestureRecognizer*)pan{	
	if(isRisize == YES){
		CGPoint oriPoint = [pan locationInView:[self superview]];
		CGSize size = [self sizeMoveViewWithWidth:self.frame.size.width + (self.frame.origin.x - oriPoint.x)
										andHeight:self.frame.size.height];
	
		[self addNewFrame:CGRectMake(self.frame.origin.x + (oriPoint.x - self.frame.origin.x), self.frame.origin.y, size.width, size.height) 
				   andVex:CGPointMake(oriPoint.x - self.frame.origin.x, oriPoint.y - (self.frame.origin.y + self.frame.size.height / 2)) 
				   andTag:8];
	}
}

#pragma mark -
#pragma mark delegate Gesture Recognizer

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
	NSLog(@"tag id %d", [touch.view tag]);
	if ([touch.view tag] != 0) {
		isRisize = YES;
	}else {
		isRisize = NO;
	}

	return YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
	//NSLog(@"should begin");
	return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
	//NSLog(@"should recognize touch");
	return YES;
}

-(void)addPanGestureRecognizerForView:(UIView*)v andMethod:(SEL)method{
	UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:method];
	pan.delegate = self;
	[v addGestureRecognizer:pan];
	[pan release];
}

-(void)addGestureRecognizerFotAllView{
	[self addPanGestureRecognizerForView:self andMethod:@selector(panMoveThisView:)];
	[self addPanGestureRecognizerForView:checkOne andMethod:@selector(panMoveViewOne:)];
	[self addPanGestureRecognizerForView:checkTwo andMethod:@selector(panMoveViewTwo:)];
	[self addPanGestureRecognizerForView:checkThree andMethod:@selector(panMoveViewThree:)];
	[self addPanGestureRecognizerForView:checkFour andMethod:@selector(panMoveViewFour:)];
	[self addPanGestureRecognizerForView:checkFive andMethod:@selector(panMoveViewFive:)];
	[self addPanGestureRecognizerForView:checkSix andMethod:@selector(panMoveViewSix:)];
	[self addPanGestureRecognizerForView:checkSeven andMethod:@selector(panMoveViewSeven:)];
	[self addPanGestureRecognizerForView:checkEight andMethod:@selector(panMoveViewEight:)];
}

-(void)addAllSubView{
	[self addSubview:checkOne];
	[self addSubview:checkTwo];
	[self addSubview:checkThree];
	[self addSubview:checkFour];
	[self addSubview:checkFive];
	[self addSubview:checkSix];
	[self addSubview:checkSeven];
	[self addSubview:checkEight];
}

#pragma mark -
#pragma mark init

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
		tagView = 0;
		isRisize = NO;
		isMove = NO;
        // Initialization code.
		checkOne = [[UIView alloc] init];
		checkTwo = [[UIView alloc] init];
		checkThree = [[UIView alloc] init];
		checkFour = [[UIView alloc] init];
		checkFive = [[UIView alloc] init];
		checkSix = [[UIView alloc] init];
		checkSeven = [[UIView alloc] init];
		checkEight = [[UIView alloc] init];
		
		[self addConfigAllView];
		[self addAllSubView];
		[self addGestureRecognizerFotAllView];
		
		//for vexter
		BPoint = self.center;
		
		//release
		[checkOne release];
		[checkTwo release];
		[checkThree release];
		[checkFour release];
		[checkFive release];
		[checkSix release];
		[checkSeven release];
		[checkEight release];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
}


@end
